/**
 */
package com.kaoshi.jeeweb.modules.sample.web;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.kaoshi.jeeweb.common.utils.StringUtils;
import com.kaoshi.jeeweb.common.web.base.BaseCRUDController;
import com.kaoshi.jeeweb.modules.sample.entity.SampleSearch;
import com.kaoshi.jeeweb.modules.sample.service.SampleSearchService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 字典Controller
 * @author ThinkGem
 * @version 2014-05-16
 */
@Controller
@RequestMapping(value = "${adminPath}/sample/search")
public class SampleSearchController extends BaseCRUDController<SampleSearch, String> {

	@Autowired
	private SampleSearchService sampleSearchService;


	@Autowired
	public void setGenericService(SampleSearchService sampleSearchService){
		super.setGenericService(sampleSearchService);
	}

	public SampleSearchController(){
		super.setListAlsoSetCommonData(true);
		setResourceIdentity("sample:search");

	}


	@ModelAttribute
	public SampleSearch get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return sampleSearchService.queryById(id);
		}else{
			return new SampleSearch();
		}
	}




	@Override
	protected void setCommonData(Model model) {
		List<String> typeList = sampleSearchService.findTypeList();
		model.addAttribute("typeList", typeList);
	}


//	@RequiresPermissions("sys:dict:view")
//	@RequestMapping(value = {"list", ""})
//	public String list(Dict dict, HttpServletRequest request, HttpServletResponse response, Model model) {
//		List<String> typeList = dictService.findTypeList();
//		model.addAttribute("typeList", typeList);
//		Page<Dict> page = dictService.findPage(new Page<Dict>(request, response), dict);
//		model.addAttribute("page", page);
//		return "modules/sys/dictList";
//	}

//	@RequiresPermissions("sys:dict:view")
//	@RequestMapping(value = "form")
//	public String form(SampleSearch sampleSearch, Model model) {
//		model.addAttribute("dict", sampleSearch);
//		return "modules/sample/searchForm";
//	}

//	@RequiresPermissions("sys:dict:edit")
//	@RequestMapping(value = "save")//@Valid
//	public String save(SampleSearch sampleSearch, Model model, RedirectAttributes redirectAttributes) {
//		if(Global.isDemoMode()){
//			addMessage(redirectAttributes, "演示模式，不允许操作！");
//			return "redirect:" + adminPath + "/sample/search/?repage&type="+ sampleSearch.getType();
//		}
//		if (!beanValidator(model, sampleSearch)){
//			return form(sampleSearch, model);
//		}
//		sampleSearchService.save(sampleSearch);
//		addMessage(redirectAttributes, "保存字典'" + sampleSearch.getLabel() + "'成功");
//		return "redirect:" + adminPath + "/sample/search/?repage&type="+ sampleSearch.getType();
//	}
	
//	@RequiresPermissions("sys:dict:edit")
//	@RequestMapping(value = "delete")
//	public String delete(SampleSearch sampleSearch, RedirectAttributes redirectAttributes) {
//		if(Global.isDemoMode()){
//			addMessage(redirectAttributes, "演示模式，不允许操作！");
//			return "redirect:" + adminPath + "/sample/search/?repage";
//		}
//		sampleSearchService.delete(sampleSearch.getId());
//		addMessage(redirectAttributes, "删除字典成功");
//		return "redirect:" + adminPath + "/sample/search/?repage&type="+ sampleSearch.getType();
//	}
	
	@RequiresPermissions("user")
	@ResponseBody
	@RequestMapping(value = "treeData")
	public List<Map<String, Object>> treeData(@RequestParam(required=false) String type, HttpServletResponse response) {
		List<Map<String, Object>> mapList = Lists.newArrayList();
		SampleSearch sampleSearch = new SampleSearch();
		sampleSearch.setType(type);
		List<SampleSearch> list = sampleSearchService.findList(sampleSearch);
		for (int i=0; i<list.size(); i++){
			SampleSearch e = list.get(i);
			Map<String, Object> map = Maps.newHashMap();
			map.put("id", e.getId());
			map.put("pId", e.getParentId());
			map.put("name", StringUtils.replace(e.getLabel(), " ", ""));
			mapList.add(map);
		}
		return mapList;
	}
	
	@ResponseBody
	@RequestMapping(value = "listData")
	public List<SampleSearch> listData(@RequestParam(required=false) String type) {
		SampleSearch sampleSearch = new SampleSearch();
		sampleSearch.setType(type);
		return sampleSearchService.findList(sampleSearch);
	}

}
